package games.eightPuzzle.heuristics;

import games.eightPuzzle.states.*;

/**
 * @author avishl02
 * this is the basic Manhattan Distances Heuristic 
 */
public class MDHeuristic implements Heuristic{



	private int [][][] _options = null;

	//ctor
	public MDHeuristic(SlidingPuzzleState finalState){
		int numbers = (finalState.getNumRows()*finalState.getNumCols())-finalState.getHolesNum();
		_options = MDHelper.buildMap(numbers, finalState);
	}


	
	
	/* (non-Javadoc)
	 * @see ai.ex1.heuristics.Heuristic#rank(ai.ex1.SlidingPuzzleState)
	 */
	public int rank(SlidingPuzzleState state){
		int sum = 0;
		int tmp;
		for(int i=0;i<state.getNumRows();i++){
			for (int j=0;j<state.getNumCols();j++){
				tmp = state.get(i,j);
				if (tmp != 0){
					sum+=_options[i][j][tmp-1];
				}
			}
		}
		return sum;
	}
}
